package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.config.EmqCode;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.LocusCoordinate;
import com.bbzn.device.client.dto.device.DevicePopupDTO;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.OptionalBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: 黄志鹏
 * @Date : 2021/4/9 11:14
 * @Version 1.0
 * NB单次定位 连续5个点
 */
@Slf4j
@Component
public class EmqGpsStrategy implements EmqStrategy {
    @Autowired
    DeviceService deviceService;

    @Autowired
    GaodeMapApiService gaodeMapApiService;

    @Autowired
    private LocusCoordinateMongoService locusCoordinateMongoService;

    @Autowired
    private PublishService publishService;

    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSON.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (!OptionalBean.ofNullable(imei).isPresent()) {
                return "json解析错误";
            }
            String lat = jsonObject.getString("lat");
            if (!OptionalBean.ofNullable(lat).isPresent()) {
                return "json解析错误";
            }
            String lon = jsonObject.getString("lon");
            if (!OptionalBean.ofNullable(lon).isPresent()) {
                return "json解析错误";
            }
            String location = jsonObject.getString("location");
            if (!OptionalBean.ofNullable(location).isPresent()) {
                return "json解析错误";
            }
            String accuracy = jsonObject.getString("accuracy");
            if (!OptionalBean.ofNullable(accuracy).isPresent()) {
                return "json解析错误";
            }
            Device device = new Device();
            device.setLon(lon);
            device.setLat(lat);
            device.setLocation(location);
            device.setImei(imei);
            deviceService.updateByImeiSelective(device);

            LocusCoordinate locusCoordinate = new LocusCoordinate();
            locusCoordinate.setLon(lon);
            locusCoordinate.setLat(lat);
            locusCoordinate.setImei(imei);
            locusCoordinate.setAccuracy(accuracy);
            locusCoordinate.setCreateTime(System.currentTimeMillis());
            locusCoordinateMongoService.recordJob(locusCoordinate);

//
//            DevicePopupDTO pop = deviceService.selDevicePopup(imei);
//            // 异常消息统一推送给web前端
//            // 组装json
//            JSONObject data = new JSONObject();
//            data.put("battery", pop.getBattery());
//            data.put("code", EmqCode.gpsCode);
//            data.put("lon", pop.getLon());
//            data.put("lat", pop.getLat());
//            data.put("name", pop.getName());
//            data.put("imei", pop.getImei());
//            data.put("headImg", pop.getHeadImg());
//            data.put("time", System.currentTimeMillis());
//            publishService.sendToMqtt(data.toJSONString(), EmqCode.sendTopic + pop.getImei() + EmqCode.endNbTopic);


        } catch (Exception e) {
            e.printStackTrace();
            return "WIFI定位失败！";
        }
        return JSON.toJSONString(payLoad);
    }
}
